package com.ys.service.base.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.base.TpAmazonRegion;
import com.ys.mapper.base.TpAmazonRegionMapper;
import com.ys.service.base.TpAmazonRegionService;
import com.ys.vo.R;

@Service("tpAmazonRegionService")
public class TpAmazonRegionServiceImpl implements TpAmazonRegionService {

	@Autowired
	private TpAmazonRegionMapper tpAmazonRegionMapper;

	/*
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params) throws Exception {
		
		TpAmazonRegion entity = JSON.parseObject(JSON.toJSONString(params), TpAmazonRegion.class);
		
		String code = entity.getCode();
		if (code == null || code.length() == 0) {
			return R.error("地区编码不能为空");
		} else if (new TpAmazonRegion().setCode(code).count() > 0) {
			return R.error("地区编码已存在");
		}

		tpAmazonRegionMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	@Transactional
	public R edit(Map<String, Object> params) throws Exception {
		TpAmazonRegion entity = JSON.parseObject(JSON.toJSONString(params), TpAmazonRegion.class);
		
		String code = entity.getCode();
		if (code == null || code.length() == 0) {
			return R.error("地区编码不能为空");
		} else if (new TpAmazonRegion().setCode(code).where("A.Id != " + entity.getId()).count() > 0) {
			return R.error("地区编码已存在");
		}
		
		tpAmazonRegionMapper.update(entity);
		return R.ok(EditDto.closeAndReload());
	}
}
